Thread: [Urgent] Can't recognize the last counter?

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    13

    Question [Urgent] Can't recognize the last counter?

    Hey all, I just joined the forum! My name is Sam.
    I have an assignment due in a few hours, and I can't seem to figure out why my C program won't read the last unsigned int.

    On line 23, the last variable (unsigned int spartyCount = 0 isn't read. Once I enter the letter M in the second switch, once I exit out (by hitting Y and the Z), the printf doesn't count up the number of times I hit M.

    The same thing happens for any int that's on the bottom of the list of integers. If wolvCount was on the bottom of the list, the letter W would not be counted into the last printf statement, either.

    I have attached the C program file below.
    Code:
    #include <stdio.h>
    
    /*
    You shall fix the following program and the program must also do the following:
        add up all of the times for Tigers, Red Wings, Pistons or school
        add a new section called S
            then ask for Grizzly, Wolverine or Sparty
        At the end of the program:
            print out how many for each catagory
            Fix the problem with the dafault option
    
    */
    
    int main( void )
    {
        char team;
        unsigned int tigerCount = 0;
        unsigned int wingCount = 0;
        unsigned int pistCount = 0;
        char team2;
        unsigned int grizzCount = 0;
        unsigned int wolvCount = 0;
        unsigned int spartyCount = 0;
    
    
        printf("Enter T for Tigers, R for Red Wings, or P for Pistons.\n");
        printf("The loop will end once Z is entered.\n");
    
        do
        {
            scanf("%s", &team);
            printf("You entered team %c.\n", team);
            switch(team)
            {
                case 'T':
                    printf("Tigers are da bomb.\n");
                    tigerCount++;
                    break;
                case 'R':
                    printf("Red Wings are da bomb.\n");
                    wingCount++;
                    break;
                case 'P':
                    printf("Piston are da bomb.\n");
                    pistCount++;
                    break;
    
                case 'S':
                    printf("Enter G for Grizzly, W for Wolverine, or M for Sparty.\n");
                    printf("This loop will end once you enter Y.\n");
                        do
                        {
                            scanf("%s", &team2);
                            printf("You entered team %c.\n", team2);
                            switch(team2)
                            {
                            case 'G':
                                printf("Grizzlies are da bomb.\n");
                                grizzCount++;
                                break;
                            case 'W':
                                printf("Wolverines are da bomb.\n");
                                wolvCount++;
                                break;
                            case 'M':
                                printf("Spartans are da bomb.\n");
                                spartyCount++;
                                break;
                            case '\n':
                            case '\t':
                            case ' ':
                                break;
                            default:
                                printf("Please enter another valid character.\n");
                                break;
                            }
                        } while (team2 != 'Y');
                        printf("Enter T for Tigers, R for Red Wings, or P for Pistons.\n");
                    break;
                case '\n':
                case '\t':
                case ' ':
                    break;
                default:
                    printf("Please enter a valid character.\n");
                    break;
            }
        } while (team != 'Z');
    
        printf("\n%u - Tigers %u - Red Wings %u - Pistons\n", tigerCount, wingCount, pistCount);
        printf("And the other schools...\n");
        printf("%u - Grizzlies %u - Wolverines %u - Spartans\n", grizzCount, wolvCount, spartyCount);
    
    }
    Attached Files Attached Files
    • File Type: c in.c (3.1 KB, 92 views)
    Last edited by Salem; 03-16-2017 at 12:28 AM. Reason: Removed useless fuzzy image and inlined the code

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > scanf("%s", &team);
    %s needs to point to at least TWO characters if you intend to store anything useful.

    > The same thing happens for any int that's on the bottom of the list of integers.
    A sure sign that data is being overwritten when you type in your responses.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    13
    Salem, how would I fix this? I'm not quite sure what you mean (I'm still very new to code).

    Changing scanf("%s... to scanf("%s%s", &team, &team2) seems wrong, since team2 is optional.

    P.S. thanks for fixing my awful thread format! I learned a little too late that you could inline code haha

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    One way - read into a string capable of holding a single character.
    char team[2];
    scanf("%s",team);

    Another way, use the %c format like so.
    char team;
    scanf(" %c",&team); // NB: leading space is very important.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2017
    Posts
    13
    Thank you so much!! The second method worked best, since constricting team to [2] strings made the switch stop working.
    I would have never guessed that you needed a space before %c. Everything now works as expected.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc and cc can't recognize includes
    By telmo_d in forum C Programming
    Replies: 3
    Last Post: 03-09-2016, 11:44 AM
  2. gcc does not recognize my lib location
    By baxy in forum C Programming
    Replies: 2
    Last Post: 08-19-2013, 11:15 AM
  3. How to recognize an image on the screen
    By lala123 in forum C Programming
    Replies: 32
    Last Post: 02-17-2010, 08:08 PM
  4. Page File counter and Private Bytes Counter
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 01-31-2008, 03:17 AM
  5. C++ code I can't recognize??
    By Guardian in forum Windows Programming
    Replies: 4
    Last Post: 04-29-2002, 04:31 PM

Tags for this Thread